//
//  HMVisitorView.swift
//  Weibo
//
//  Created by 黄亦寒 on 16/9/20.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
//协议
protocol HMVisitorViewDelegate:NSObjectProtocol{
    //代理方法
    func loginBtnClick()
}

class HMVisitorView: UIView {

    //回调的闭包
    var loginClosure:(()->())?
    
    //代理属性
    weak var delegate:HMVisitorViewDelegate?
    //懒加载控件
    //旋转视图
    private lazy var cycleImageView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    //罩层
    private lazy var maskImageView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //房子
    private lazy var houseImageView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_house"))
    
    //文字信息
    private lazy var textLabel:UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    
    }()
    
    //登录按钮
    private lazy var loginButton:UIButton = {
        let btn = UIButton()
        //添加点击事件
        btn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //设置按钮背景图片
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //取消按钮点击效果
        btn.adjustsImageWhenHighlighted = false
        
        return btn
    }()
    
    //注册按钮
    private lazy var registerButton:UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        btn.adjustsImageWhenHighlighted = false
        
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() {
        backgroundColor = UIColor(white:237/255,alpha:1)
        //添加控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(textLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //控件布局
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            }
        
            maskImageView.snp_makeConstraints { (make) in
                make.center.equalTo(cycleImageView)
        }
        
        houseImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        textLabel.snp_makeConstraints { (make) in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(230)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            make.leading.equalTo(textLabel)
            make.width.equalTo(80)
            make.height.equalTo(35)
        }
        
        registerButton.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(textLabel)
            make.top.width.height.equalTo(loginButton)
            
        }
        
        }
    
    //修改访客视图信息
    func updateVisitorInfo(message:String?,imageName:String?) {
        //修改消息，发现，我的三个控制器的访客信息
        if let msg = message,let imgName = imageName {
            textLabel.text = msg
            houseImageView.image = UIImage(named:imgName)
            //取消旋转视图
            cycleImageView.isHidden = true
        }else{
            //开启首页控制器的旋转视图动画
            startAnimation()
        }
    }
    
    //首页控制器的旋转视图动画方法
    func startAnimation() {
        //创建核心动画对象
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        //旋转360度
        animation.toValue = 2 * M_PI
        //动画时长
        animation.duration = 20
        //动画执行次数
        animation.repeatCount = MAXFLOAT
        //让动画因切换到别的界面也继续保持执行
        animation.isRemovedOnCompletion = false
        //添加动画到旋转视图
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    //按钮的点击事件
    func loginClick() {
        //执行闭包
        loginClosure?()
        //执行代理方法
        delegate?.loginBtnClick()
    }
    
    }



